package net.takela.common.security.handler;

import org.springframework.security.access.AccessDecisionVoter;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;

public class PermissionVoter implements AccessDecisionVoter<Object> {
    
    /**
     * 
     */
    @Override
    public boolean supports(ConfigAttribute attribute) {
        return true;
    }

    
    /**
     * 
     */
    @Override
    public int vote(Authentication authentication, Object object, Collection<ConfigAttribute> attributes){
        if (authentication == null) {
            return ACCESS_DENIED;
        }
//        if (true) {
//            return ACCESS_GRANTED;
//        }
        int result = ACCESS_ABSTAIN;
        Collection<? extends GrantedAuthority> authorities = extractAuthorities(authentication);
        for (ConfigAttribute attribute : attributes) {
            if (this.supports(attribute)) {
                result = ACCESS_DENIED;
                // Attempt to find a matching granted authority
                for (GrantedAuthority authority : authorities) {
                    if (attribute.getAttribute().equals(authority.getAuthority())) {
                        return ACCESS_GRANTED;
                    }
                }
            }
        }
        return result;
    }
    
    /**
     * 
     */
    @Override
    public boolean supports(Class clazz) {
        return true;
    }
    Collection<? extends GrantedAuthority> extractAuthorities(Authentication authentication) {
        return authentication.getAuthorities();
    }
}
